import React, { Component } from 'react'
import axios from 'axios';
import { withRouter } from 'react-router-dom';

import { connect } from "react-redux";
import { loadUseinfoData } from '../../redux/reducers/user.reducer.js';
// 控制路由组件
class AuthRouter extends Component {
    render() {
        return (
            <div>
                
            </div>
        )
    }

    // 组件挂载完成
    componentDidMount(){
        const pathArr = ['/','/login','/regist'];
        const pathName = this.props.location.pathname;
        // 是登录/注册页
        if(pathArr.indexOf(pathName)>-1){
            return null
        }else{
            // 不是登录/注册页 获取用户信息 (需要cookie-》登录)
            axios.get('/userapi/info').then(res => {
                if(res.status === 200){
                    // 有cookie，已经登陆过
                    if(res.data.code === 0){
                        return null
                    }else{
                        // 得不到用户信息-》没有登陆
                        this.props.history.push('/');
                    }
                }
            })
        }
    }
}

export default withRouter(connect(
    null,
    {loadUseinfoData}
)(AuthRouter));

/**【vue路由拦截】
 * router.beforeResolve((to, from, next) => {
    // 解决
    if (to.path === '/login') {
        NProgress.start();
        next()
        return
    }
    if (store.getters.token) {
        if (to.path === '/') {
            next({
                path: '/'
            })
        } else {
            NProgress.start();
            next()
        }
    } else {
        NProgress.start();
        next({
            path: '/login'
        })
    }
});
 */